<style scoped>
  .weui-form-preview__hd label, .weui-form-preview__hd em {
    font-size: .9em !important;
  }
  .weui-loadmore_line .weui-loadmore__tips {
    background-color: #eee;
  }
  .weui-cells {
    margin-top: 0;
  }
  .weui-cells_form {
    background: #fbfbfb;
  }
  .weui-cell {
    background: #fff;
  }
  .weui-cells__title {
    text-align: left;
  }
  .oitem {
    padding: 3px;
  }
  .hide {
    display: none;
  }
  #acts {
    font-size: 0;
  }
  #acts span {
    font-size: 14px;
  }
  .halfbtn {
    display: inline-block;
    width: 49%;
  }
  .weui-label {
    text-align: left;
  }
  .weui_cell {
    padding: 10px 15px;
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
    background: #fff;
  }
  .weui-cell_select {
    padding: 0 15px;
  }
  .rj-cont-tit {
    margin: 0;
    border-radius: 0;
  }

  .weui-panel {
    /*background: #eee;*/
    margin-top: 0;
    text-align: left;
  }

  .hd .weui-grid.js_grid {
    font-weight: 600;
  }

  .weui-grid.js_grid {
    width: 25%;
    padding: 10px 1px;
  }

  /*.weui-grid.fir {*/
  /*  width: 20%;*/
  /*}*/
  /*.weui-grid.sec {*/
  /*  width: 15%;*/
  /*}*/
  /*.weui-grid.thr {*/
  /*  width: 20%;*/
  /*}*/
  /*.weui-grid.fou {*/
  /*  width: 15%;*/
  /*}*/
  /*.weui-grid.fiv {*/
  /*  width: 15%;*/
  /*}*/

  .weui_uploader_input_wrp {
    float: left;
    position: relative;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 77px;
    height: 77px;
    border: 1px solid #D9D9D9;
  }

  .weui_uploader_hd {
    padding-top: 0;
    padding-right: 0;
    padding-left: 0;
  }

  .weui_uploader_files {
    list-style: none;
  }

  .weui_uploader_input_wrp:before {
    width: 2px;
    height: 39.5px;
  }

  .weui_uploader_input_wrp:before, .weui_uploader_input_wrp:after {
    content: " ";
    position: absolute;
    top: 50%;
    left: 50%;
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    background-color: #D9D9D9;
  }

  .weui_uploader_input {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  }

  .weui_uploader_input_wrp:after {
    width: 39.5px;
    height: 2px;
  }

  .weui_uploader_bd {
    overflow: hidden;
  }

  .weui_cell {
    padding: 10px 15px;
    position: relative;
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;
  }

  .outstore >>> .weui-uploader_files {
    list-style: none;
    overflow-y: hidden;
  }

  .outstore >>> .weui-uploader_file {
    float: left;
    margin-right: 9px;
    margin-bottom: 9px;
    width: 79px;
    background: no-repeat 50%;
    background-size: cover;
  }

  .weui-cell.fir {
    border-top: 0;
  }

  .help {
    text-align: right;
    padding: 5px;
    font-size: 12px;
    color: #bbb;
  }

  .weui_uploader_input_wrp.inrow {
    width: 30px;
    height: 30px;
    margin: 0;
  }

  .weui_uploader_input_wrp:before {
    height: 19.5px;
  }

  .weui_uploader_input_wrp:after {
    width: 19.5px;
  }

  .weui-uploader_file.inrow {
    margin: 0;
    width: 40px;
  }

  li.weui-uploader_file {
    display: inline-block;
    padding: 3px 5px;
  }

  ul#images {
    text-align: right;
  }
  .weui-panel__bd {
    width: auto !important;
    padding: 10px 15px;
    text-align: left;
  }
  .rj-cell {
    font-size: 0.9em;
    display: inline-block;
    margin-right: 15px;
  }
  .section {
    width: auto;
    margin: 5px;
    border-radius: 5px;
    text-align: left;
    overflow: hidden;
  }
  .itemSection {
    background: #fafafa;
    margin: 3px;
  }
</style>
<template>
  <div class="examine" style="background: #eee;">
    <div class="rj-wrap" style="background: #fff;">
      {{title}}
      <a href="javascript:;" @click="$router.back(-1)" style="position: absolute; left: 10px; top: 8px">
        <img src="@/assets/back.png" style="width: 20px" />
      </a>
    </div>
    <!--    主表信息-->
    <div class="weui-form-preview section" style="background: #fff;">
      <div class="weui-form-preview__hd">
        <label class="weui-form-preview__label">编号</label>
        <em class="weui-form-preview__value">{{baseInfo.sellcode}}</em>
      </div>
      <div class="weui-form-preview__bd">
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">申请人</label>
          <span class="weui-form-preview__value">{{baseInfo.applyname}} {{baseInfo.depname}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">客户</label>
          <span class="weui-form-preview__value">{{baseInfo.customername}}</span>
          <span v-if="customer != null">{{baseInfo.typename}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">货品类型</label>
          <span class="weui-form-preview__value">{{baseInfo.quitSRTypeName}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">付款方式</label>
          <span class="weui-form-preview__value">{{baseInfo.paytypename}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">订单类型</label>
          <span class="weui-form-preview__value">{{baseInfo.flagname}}{{baseInfo.flag == 3 ? '[' + baseInfo.parentcode + ']' : ''}}</span>
        </div>
        <div class="weui-form-preview__item" v-if="flowid == 36 && baseInfo.faretypename">
          <label class="weui-form-preview__label">物流结算</label>
          <span class="weui-form-preview__value">{{baseInfo.faretypename}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">配送方式</label>
          <span class="weui-form-preview__value">{{baseInfo.deliverytypename}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">要求发货日期</label>
          <span class="weui-form-preview__value">{{baseInfo.senddate}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">要求到货日期</label>
          <span class="weui-form-preview__value">{{baseInfo.arrivedate}}</span>
        </div>
<!--        <div class="weui-form-preview__item" v-if="baseInfo.settletype == 1">-->
<!--          <label class="weui-form-preview__label">订单件数</label>-->
<!--          <span class="weui-form-preview__value">{{baseInfo.orderpack}}</span>-->
<!--        </div>-->
<!--        <div class="weui-form-preview__item" v-if="baseInfo.settletype == 1 && baseInfo.handselcount != ''">-->
<!--          <label class="weui-form-preview__label">赠送件数</label>-->
<!--          <span class="weui-form-preview__value">{{baseInfo.handselcount}}</span>-->
<!--        </div>-->
<!--        <div class="weui-form-preview__item">-->
<!--          <label class="weui-form-preview__label">订单重量</label>-->
<!--          <span class="weui-form-preview__value">{{baseInfo.orderweight}} 吨</span>-->
<!--        </div>-->
<!--        <div class="weui-form-preview__item" v-if="baseInfo.handselweight != '' && baseInfo.handselweight != null">-->
<!--          <label class="weui-form-preview__label">赠送重量</label>-->
<!--          <span class="weui-form-preview__value">{{baseInfo.handselweight}} 吨</span>-->
<!--        </div>-->
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">结算币种</label>
          <span class="weui-form-preview__value">{{baseInfo.currencyName}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">状态</label>
          <span class="weui-form-preview__value">{{baseInfo.statusname}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">备注</label>
          <span class="weui-form-preview__value">{{baseInfo.remarks}}</span>
        </div>
        <div class="weui-form-preview__item">
          <label class="weui-form-preview__label">创建时间</label>
          <span class="weui-form-preview__value">{{baseInfo.createtime}}</span>
        </div>
        <div class="weui-form-preview__item" v-if="baseInfo.files && baseInfo.files.length > 0">
          <label class="weui-form-preview__label">附件</label>
          <a v-for="(item, index) in baseInfo.files" :href="item.url">
            <span class="weui-form-preview__value">{{item.imagename}}</span>
          </a>
        </div>
      </div>
    </div>
    <!--    明细信息-->
<!--    <div class="weui-panel__hd" style="text-align: left;">货品明细</div>-->
    <div class="" v-if="spitems && spitems.length > 0" v-for="(pack, packindex) in spitems">
      <div class="weui-cells__title">货品明细{{packindex + 1}}
        <p style="float: right;color: #1aad19;">
          <span style="padding-left: 5px;" @click="delPackItem(pack.guid)" v-if="totalItemsLen > 1">删除</span>
        </p>
      </div>
      <div class="weui-panel weui-panel_access section fir" style="text-align: left;margin-top: 0;background: #fff;">
        <div class="weui-panel__hd" style="font-weight: 600;color: #333;font-size: 14px;">{{pack.productname}} {{pack.spname}}</div>
        <div class="weui-cell" style="border-radius: 5px 5px 0 0;">
          <div class="weui-cell__hd"><label class="weui-label">订单数量</label></div>
          <div class="weui-cell__bd">
            <input class="weui-input inlinput1" type="text" name="" readonly="true" :value="pack.orderpack">
            <input class="weui-input inlinput2" type="text" readonly :value="pack.unit">
          </div>
          <template v-if="baseInfo.settletype == 1 && baseInfo.paytype < 4">
            <div class="weui-cell__hd"><label class="weui-label" style="width: 50px;">赠送</label></div>
            <div class="weui-cell__bd">
<!--              <input class="weui-input givecount inlinput1" type="number" readonly :value="pack.handselcount">-->
              <input class="weui-input givecount inlinput1" type="number" readonly :value="pack.handselcount">
              <input class="weui-input inlinput2" type="text" readonly :value="pack.unit">
            </div>
          </template>
        </div>
        <div class="weui-cell">
          <div class="weui-cell__hd"><label class="weui-label">出库数量</label></div>
          <div class="weui-cell__bd">
            <input class="weui-input packOutcount" :data-index="packindex" style="width: 80%;display: inline-block;" type="number" placeholder="订单数量+赠送数量" @change="takeLoadcount($event, packindex)">
            <input class="weui-input" style="width: 15%;display: inline-block;" type="text" readonly :value="pack.unit">
          </div>
        </div>
        <div class="weui-panel__bd" style="padding-left: 15px;">
          <div v-for="(product, index) in pack.sellitems" class="weui-panel weui-panel_access" style="text-align: left;margin-top: 0;">
            <div class="weui-panel__hd">
              <span style="display: inline-block;width: 70%;color: #333;">{{product.productname}} {{product.spname}}</span>
              <input class="weui-input outcount" style="display: inline-block;width: 8%;" type="number" readonly :data-index="index" :data-unit="product.packweightitem" :data-netweight="product.netweight" :data-whinunit="product.whinunitName" :data-packamount="product.packamount" :value="model.items[addBasc3(packindex, index)].weight">
              <span>{{product.unit}}</span>
            </div>
            <div class="weui-panel__bd">
              <a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg" style="padding: 0;">
                <div class="weui-media-box__bd">
                  <div class="weui-media-box__desc">
                    <div class="weui-cell" style="padding: 0 15px;">
                      <div class="weui-cell__hd"><label class="weui-label">库房</label></div>
                      <input class="weui-input" type="text" name="" readonly="true" :value="baseInfo.storeroomname">
                    </div>
                  </div>
                </div>
              </a>
            </div>
            <div class="weui-panel__bd">
              <a href="javascript:void(0);" class="weui-media-box weui-media-box_appmsg" style="padding: 0;">
                <div class="weui-media-box__bd">
                  <div class="weui-media-box__desc">
                    <div class="weui-cell" style="padding: 0 15px;">
                      <div class="weui-cell__hd"><label class="weui-label">库间</label></div>
                      <select class="weui-select houseroom" v-model="model.items[addBasc3(packindex, index)].shrguid" >
                        <option value="" disabled>请选择库间</option>
                        <option v-for="(item, i) in autoHouses(addBasc3(packindex, index))" :key="item.storeRhGuid" :value="item.storeRhGuid" :data-storeqty="item.storeqty" :data-house="item.storeRhName">{{item.storeRhName}} ({{item.storeqty}}{{item.stockunit}})</option>
                      </select>
                    </div>
                  </div>
                </div>
              </a>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="" v-for="(product, index) in sellitems">
      <div class="weui-cells__title">货品明细{{addBasc2(index) + 1}}
        <p id="acts" style="float: right;color: #1aad19;">
          <span style="padding-left: 5px;" @click="delItem(product.guid)" v-if="totalItemsLen > 1">删除</span>
        </p>
      </div>
      <div class="section"  style="background: #fff;">
        <div class="weui-cell fir" style="border-radius: 5px 5px 0 0;">
          <div class="weui-cell__hd"><label class="weui-label">产品</label></div>
          <div class="weui-cell__bd">
            <input class="weui-input" type="text" name="" readonly="true" :value="product.productname + product.spname">
          </div>
        </div>
        <div class="weui-cell">
          <div class="weui-cell__hd"><label class="weui-label">库房</label></div>
          <div class="weui-cell__bd">
            <input class="weui-input" type="text" name="" readonly="true" :value="baseInfo.storeroomname">
          </div>
        </div>
        <div class="weui-cell">
          <div class="weui-cell__hd"><label class="weui-label">库间</label></div>
          <div class="weui-cell__bd">
            <input type="hidden" ref="fromshname" name="fromshname">
            <select class="weui-select house" v-model="model.items[addBasc(index)].shrguid">
              <option value="" disabled>请选择</option>
              <option v-for="(item, i) in autoHouses(addBasc(index))" :key="item.storeRhGuid" :value="item.storeRhGuid" :data-storeqty="item.storeqty" :data-house="item.storeRhName">{{item.storeRhName}} ({{item.storeqty}}{{item.stockunit}})</option>
            </select>
          </div>
        </div>
<!--        <template v-if="baseInfo.settletype == 1">-->
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label" style="width: 80px;">订单数量</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input" style="width: 50%;display: inline-block;" type="text" name="" readonly="true" :value="product.weight">
              <input class="weui-input" style="width: 30%;display: inline-block;" type="text" readonly :value="product.unit">
            </div>
            <template v-if="baseInfo.settletype == 1 && baseInfo.paytype < 4">
              <div class="weui-cell__hd"><label class="weui-label" style="width: 50px;">赠送</label></div>
              <div class="weui-cell__bd">
                <input class="weui-input givecount" style="width: 50%;display: inline-block;" type="number" readonly :value="product.handselcount ? product.handselcount : 0">
                <input class="weui-input" style="width: 30%;display: inline-block;" type="text" readonly :value="product.unit">
              </div>
            </template>
          </div>
          <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">出库数量</label></div>
            <div class="weui-cell__bd">
              <input class="weui-input outcount" style="width: 80%;display: inline-block;" :data-index="index" :data-unit="product.netweight" type="number" pattern="[0-9]*" placeholder="订单数量+赠送数量" v-model="model.items[addBasc(index)].weight" @change="takeItemWei($event, addBasc(index), index)">
              <input class="weui-input" style="width: 15%;display: inline-block;" type="text" readonly :value="product.unit">
            </div>
          </div>
<!--        </template>-->
<!--        <template v-if="baseInfo.settletype == 2">-->
<!--          <div class="weui-cell">-->
<!--            <div class="weui-cell__hd"><label class="weui-label" style="width: 80px;">订单数量</label></div>-->
<!--            <div class="weui-cell__bd">-->
<!--              <input class="weui-input" style="width: 50%;display: inline-block;" type="text" name="" readonly="true" :value="product.weight">-->
<!--              <input class="weui-input" style="width: 30%;display: inline-block;" type="text" readonly :value="product.unit">-->
<!--            </div>-->
<!--          </div>-->
<!--          <div class="weui-cell">-->
<!--            <div class="weui-cell__hd"><label class="weui-label">出库数量</label></div>-->
<!--            <div class="weui-cell__bd">-->
<!--              <input class="weui-input outcount" style="width: 80%;display: inline-block;" :data-index="index" :data-unit="product.netweight" type="number" pattern="[0-9]*" placeholder="订单数量" v-model="model.items[addBasc(index)].weight" @change="toSumWeight">-->
<!--              <input class="weui-input" style="width: 15%;display: inline-block;" type="text" readonly :value="product.unit">-->
<!--            </div>-->
<!--          </div>-->
<!--        </template>-->
      </div>
    </div>
    <!--    动态数据-->
    <p style="height: 5px;"></p>
      <div class="weui-cells weui-cells_form section">
      <form action="" method="post" id="formid">
        <div class="weui-cell fir">
          <div class="weui-cell__hd"><label class="weui-label">附件上传</label></div>
          <div class="weui-cell__ft">
            <div class="weui_uploader_bd">
              <ul class="weui_uploader_files"></ul>
              <div class="weui_uploader_input_wrp inrow">
                <UploadFile3 class="weui_uploader_input" v-on:pushfile="setFile"></UploadFile3>
              </div>
            </div>
          </div>
          <ul class="weui-uploader_files" id="images">
            <li class="weui-uploader_file" v-for="(fil, index) in filenames" @click=delFile(fil,fileids[index])>
              {{fil}}
            </li>
          </ul>
        </div>
        <div class="weui-cell">
          <div class="weui-cell__bd">
            <textarea class="weui-textarea opinion" id="opinion" v-model="opinion" name="opinion" placeholder="请输入意见内容" rows="2"></textarea>
          </div>
        </div>
      </form>
    </div>
    <div id="bmlogs">
      <SalesOperationLogs7 :nodeid="nodeid" :orderid="id" :srguid="baseInfo.srguid" :departid="baseInfo.depguid" :flowid="flowid"></SalesOperationLogs7>
    </div>
      <ApproveBom v-if="isReady" type="sellOut" :flowid="flowid" :nodeid="nodeid" :id="id" :srguid="baseInfo.srguid" :departid="baseInfo.depguid" :model="model" :opinion="opinion" :fileids="fileids" :refuseflag="refuseflag" :annex="annex" passUrl="/prod-api/bill/sellform/whout" refuseUrl="/prod-api/bill/sellform/refuse"  :navgateUrl="`/sellform2/SellformList?flowid=${flowid}`" ></ApproveBom>
  </div>
</template>

<script>
  import wx from 'weixin-js-sdk';
  import SalesOperationLogs7 from '@/components/common/SalesOperationLogs7'
  import ApproveBom from '@/components/common/ApproveBom'
  import UploadFile3 from "../common/UploadFile3";

  export default {
  name: 'SellformOut',
  data () {
    return {
      flowid: 34,
      opinion: '',
      openid: localStorage.openid,
      id: '',
      title: '',
      nodeid: '',
      annex: '',
      baseInfo: {},
      baseItems: '',
      wxParams: {},
      images: {
        serverId: [],
        srcStr: [],
      },
      refuseflag: '',
      refuseNodes: [],
      orderfiles: [],
      isReady: false,
      filenames: [],
      fileids: [],
      titleName:"",
      houses: [],
      model: {
        totalRealWei: 0,
        items: []
      },
      sellitems: [],
      allItems: [],
      spitems: [],
      totalPackitemLen: 0,
      totalItemsLen: 0,
      delPItemIndexs: [],
      delItemIndexs: [],
      artoken: '',
      editRoomIndexs: [],
      mergeSpitems: [],
    }
  },
    components: {
      SalesOperationLogs7,
      ApproveBom,
      UploadFile3
    },
    computed: {
      pageTitle: function () {
        let tit = '销售订单';
        switch (this.flowid) {
          case '34':
            tit = '电商订单';
            break;
          case '35':
            tit = '样品订单';
            break;
          default:
            tit = '销售订单';
            break;
        }
        return tit;
      }
    },
    created() {
      if (this.$route.query.flowid) this.flowid = this.$route.query.flowid;
      //页面title设置
      document.title = '土豆集-' + this.pageTitle;
    },
  mounted () {
    //提交操作定位到页面底部
    //$('#bmlogs').css('margin-bottom', this.$refs.actHtml.offsetHeight)

    //$('#opinion').focus();
    this.id = this.$route.query.id;
    this.nodeid = this.$route.query.nodeid;
    this.annex = this.$route.query.annex;
    this.title = this.$route.query.title;
    this.refuseflag = this.$route.query.refuseflag;

    this.getBaseInfo(this.id);
    if (this.$route.query.status) {
      this.status = this.$route.query.status;
    }
  },
  methods: {
    toSumWeight: function(val) {
      this.model.totalRealWei = this.model.items.reduce((groups, item)=>{
        return groups + parseFloat(item.weight);
      }, 0);
    },
    takeLoadcount: function($event, packindex) {
      let that = this;
      let handselcount = that.spitems[packindex].handselcount ? that.spitems[packindex].handselcount : 0;
      if ($event.target.value > that.spitems[packindex].orderpack + handselcount) {
        this.toastText('出库数量不能大于 订单数量+赠送数量！');
        setTimeout(() => {
          $event.target.value = '';
        }, 2000)
        return;
      }
      this.spitems[packindex].sellitems.forEach((item, index) =>{
        this.$set(this.model.items[this.addBasc3(packindex, index)], 'weight', $event.target.value * (this.spitems[packindex].sellitems[index].weight / this.spitems[packindex].orderpack));
      })
    },
    takeItemWei: function($event, index2, index) {
      let that = this;
      if (this.baseInfo.settletype == 1) {
        let handselcount = that.sellitems[index].handselcount ? that.sellitems[index].handselcount : 0;
        if (this.model.items[index2].weight > that.sellitems[index].weight + handselcount) {
          this.toastText('出库数量不能大于 订单数量+赠送数量！');
          setTimeout(() => {
            this.model.items[index2].weight = '';
          }, 2000)
        }
      } else if (this.baseInfo.settletype == 2) {
        if (this.model.items[index2].weight > that.sellitems[index].weight + parseFloat(this.model.weightx * that.sellitems[index].weight)) {
          this.toastText('出库数量不能超出订单数量！');
          //   this.toastText('出库数量有误，不允许大于订单数量：'+ that.sellitems[index].weight + ' + 系统设定浮动数量：' + this.toFix6(this.model.weightx * that.sellitems[index].weight) + '(=' + this.model.weightx + '*' + that.sellitems[index].weight + ')');
          setTimeout(() => {
            this.model.items[index2].weight = '';
          }, 2000)
        }
      }
    },
    delPackItem: function(id) {
      let that = this;
      $.confirm('确定要删除该明细？', function () {
        that.spitems = that.spitems.filter(item => item.guid != id);
        // that.$nextTick(function () {
        that.getMutilHouse();
        // })
        that.totalItemsLen = that.spitems.length + that.sellitems.length;
        that.setModelData();
        that.resetSpitemsLength();
        that.$forceUpdate();
      })
    },
    delItem: function (id) {
      let that = this;
      $.confirm('确定要删除？', function () {
        that.sellitems = that.sellitems.filter(item => item.guid != id);
        // that.model.items = that.model.items.filter(item => item.guid != id);
        // that.$nextTick(function () {
        that.getMutilHouse();
        // })
        that.totalItemsLen = that.spitems.length + that.sellitems.length;
        that.setModelData();
        that.$forceUpdate();
      })
    },
    autoHouses: function(no) {
      return this.houses['house' + no];
    },
    getWei: function(val, wei, index) {
      if (val.target.value > wei * (1 + this.tranferParam / 100)) {
        this.toastText('出库重量不能大于: 订单重量 * ' + (100+this.tranferParam) + '%');
        this.$refs['weight' + index][0].value = '';
      }
    },
    delFile: function (name, id) {
      let that = this;
      $.confirm('确定要删除？', function () {
        that.axios({
          method: 'post',
          url: '/prod-api/common/delete',
          params: {id: id}
        })
        that.fileids = that.fileids.filter(item => {
          return item != id;
        })
        that.filenames = that.filenames.filter(item => {
          return item != name;
        })
      })
    },
    setFile: function (file) {
      this.filenames.push(file.name);
      this.fileids.push(file.id);
    },
    packUnit: function(unit, currency) {
      return unit != undefined && unit != '' ? currency == 2 ? '美元/' + unit : '元/' + unit : '';
    },
    checkCore:function() {
      var u = navigator.userAgent;
      //var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1;//g
      var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);   //ios终端
      if (isIOS) {
        return 'ios';
      } else  {
        return 'android';
      }
    },
    getMutilHouse: function(orgid = '') {
      //套装货品明细
      this.spitems.filter((item, packindex) =>{
        item.sellitems.filter((pro, index) => {
          this.getHouses(this.baseInfo.storeOrgId, this.baseInfo.strmId, this.spitems[packindex].sellitems[index].productguid, this.spitems[packindex].sellitems[index].spguid, this.addBasc3(packindex, index));
        })
      })
      //非套装货品明细
      for(let i=0; i<this.sellitems.length; i++) {
        this.getHouses(this.baseInfo.storeOrgId, this.baseInfo.strmId, this.sellitems[i].productguid, this.sellitems[i].spguid, this.totalPackitemLen + i);
      }
    },
    addBasc: function(index) {
      return this.totalPackitemLen + parseInt(index);
    },
    addBasc2: function(index) {
      return this.spitems.length + parseInt(index);
    },
    addBasc3: function(packindex, index) {
      let no = 0;
      for (let i=0; i<packindex; i++) {
        no += this.spitems[i].sellitems.length;
      }
      return no + parseInt(index);
    },
    setModelData: function() {
      let spsellitems = [];
      if (this.spitems) {
        spsellitems = this.spitems.reduce((groups, item)=>{
          return groups.concat(item.sellitems);
        }, [])
      }
      this.allItems = spsellitems.concat(this.sellitems);
      this.model.items = this.allItems.map(item => {
        let nitem = {};
        nitem.itemguid = item.guid;
        nitem.spguid = item.speguid ? item.speguid : item.spguid;
        nitem.productguid = item.productid ? item.productid : item.productguid;
        // nitem.orderpack = item.orderpack;
        // nitem.handselcount = item.handselcount;
        nitem.weight = '';
        nitem.handselcount = item.handselcount;
        nitem.sfunitid = item.sfunitid;
        nitem.price = item.price;
        nitem.shrguid = '';
        return nitem;
      })
    },
    resetSpitemsLength: function() {
      this.totalPackitemLen = 0;
      this.spitems.forEach(item => {
        this.totalPackitemLen += item.sellitems.length;
      })
    },
    getBaseInfo: function (id) {
      let hthis = this;
      this.axios({
        method: 'get',
        url: `/prod-api/bill/sellform/detail`,
        params: {orderid: id}
      }).then(response => {
        if (response.data.code == 200) {
          let data = response.data.data;
          this.baseInfo = data.result || {};
          this.sellitems = this.baseInfo.items;

          this.spitems = this.baseInfo.spitems;
          this.totalItemsLen = this.sellitems.length + this.spitems.length;
          this.resetSpitemsLength();
          // this.$nextTick(function () {
              this.getMutilHouse();
              this.setModelData();
          // })

          this.model.srguid = this.baseInfo.storermguid;

          this.model.settletype = this.baseInfo.settletype;
          if (this.baseInfo.settletype == 2) {
            this.model.orderweight = this.baseInfo.orderweight;
            this.getSysParam();
          }
          //获取库间
          // let items = this.model.items;
          // if (items.length > 0) {
          //   for (let i=0; i<items.length; i++) {
          //     this.getHouses(this.baseInfo.quitsrtype, this.baseInfo.storeOrgId, this.baseInfo.strmId, items[i].productguid, items[i].spguid, i);
          //   }
          // }
        } else if (response != undefined && response.data != undefined && response.data.msg == 'sessionTimeOut') {
          //不不设置提示
        } else {
          this.toastError('系统错误');
        }
        this.isReady = true;
      });
    },
    getSysParam: function() {
      this.axios({
        method: 'get',
        url: '/prod-api/system/config/configKey/sellfor_woratio',
      }).then((response) => {
        if (response.data.code == 200) {
          this.model.weightx = parseFloat(response.data.msg);
        } else if (response.data.msg) {
          this.toastText(response.data.msg);
        }
      })
    },
    getHouses: function(orgid, roomid, productid, specid = '', index) {
      this.axios({
        method: 'get',
        url: '/prod-api/getPLInfo',
        params: {strmid: roomid, productid: productid, spec: specid, orgid: orgid}
      }).then((response) => {
        if (response.data.code == 200) {
          let data = response.data;
          this.$set(this.houses, 'house' + index, data.data);
          //this['house' + index] = data.houses;
        } else if (response.data.msg) {
          this.toastText(response.data.msg);
        } else {
          this.toastText("获取库间数据失败");
        }

      })
    },


}
}
</script>
